<?php
namespace App\Http\Controllers\Admin;

use App\Service\Admin\FindService;
use Illuminate\Http\Request;

class FindController extends BaseController
{
    //发现话题

    public function __construct(FindService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getIndex(Request $request)
    {
        $res = $this->db->getList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //审核
    public function setAuth(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->setAuth($id);
        if ($res['code'] < 0) {
            return $this->toJson($res['code'], $res['msg']);
        }
        return $this->toJson();
    }

    //一个
    public function getOne(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //删除
    public function setDel(Request $request)
    {
        $id = $request->post('id', 0);
        if (!$id) {
            return $this->toJson(-2, '门店ID必须的');
        }
        $res = $this->db->setDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //列表
    public function cateList(Request $request)
    {
        $res = $this->db->cateList();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //修改
    public function cateModify(Request $request)
    {
        $id = $request->post('id', 0);
        $pas['parent_id'] = $request->post('pid', 0);
        $pas['cate_name'] = $request->post('title', '');
        $res = $this->db->cateModify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function cateDel(Request $request)
    {
        $id = $request->post('id', 0);
        $res = $this->db->cateDel($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //是否置顶
    public function setTop(Request $request)
    {
        $id = $request->post('id', 0);
        $top = $request->post('top', 0);
        $res = $this->db->setTop($id, $top);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //开关获取
    public function switchOne(Request $request)
    {
        $res = $this->db->switchOne();
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //开关设置
    public function switchModify(Request $request)
    {
        $kg = $request->post('kg', '');
        if (!in_array($kg, [0, 1])) {
            return $this->toJson(-2, '参数错误');
        }
        $res = $this->db->switchModify($kg);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}